<template>
  <div class="app-container">
    <div id="head">
      <div class="block">
        <el-form :inline="true" :model="classify" class="demo-form-inline">
          <el-form-item label="商品名称">
            <el-input v-model="classify.name" clearable placeholder="商品名称" />
          </el-form-item>
          <el-form-item class="ml-1" label="分类列表">
            <el-select v-model="classify.classifyId" placeholder="选择分类" clearable>
              <el-option v-for="(item,index) in classifyids" :key="index" :label="item.classifyName" :value="item.id" /></el-select>
          </el-form-item>
          <el-form-item class="ml-1" label="商品状态">
            <el-select v-model="classify.status" placeholder="选择商品状态" clearable>
              <el-option v-for="(item,index) in options" :key="index" :label="item.label" :value="item.value" /></el-select>
          </el-form-item>
          <el-button type="primary" class="ml-1" @click="search">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addad('ruleForm')">添加商品</el-button>
          <el-button v-show="delshow" type="danger" icon="el-icon-delete" @click="delselect">批量删除</el-button>
        </el-form>
      </div>
      <el-dialog top="10vh" title="添加商品" width="35%" :visible.sync="dialogFormVisible" :before-close="handleClose" @click="getcateid">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-radio-group v-model="radio" style="height:45px" name="form1">
              <el-radio :label="3" class="mt-1" @change="inpage1">无限制年龄</el-radio>
              <el-radio :label="6" @change="inpage2">有限制年龄</el-radio>
              <br>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="show4" style="margin-bottom: 20px" :label-width="formLabelWidth">
            <el-input v-model="form.input1" placeholder="请输入年龄" autocomplete="off" />
          </el-form-item>
          <!--          <p ref="bg" style="height: 10px;color:red;margin-left: 80px;margin-top: -20px;font-size: 10px" v-if="form.input1 === ''">年龄不能为空</p>-->
          <el-form-item label="分类列表" :label-width="formLabelWidth" prop="classifyName">
            <el-select v-model="form.id" placeholder="请选择分类列表">
              <el-option v-for="(item,index) in classifyids" :key="index" :label="item.classifyName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="商品名" style="margin-bottom: 10px" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品图" style="margin-bottom: 20px" :label-width="formLabelWidth" prop="logo">
            <el-upload
              ref="upload"
              class="avatar-uploader"
              action="https://app.xiangban365.com/app_mobile/upload/file"
              :show-file-list="false"
              :on-error="handleAvatarError"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else style="border: 1px dashed #d9d9d9" class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="价格" style="margin-bottom: 20px" :label-width="formLabelWidth" prop="price">
            <el-input
              v-model="form.price"
              type="text"
              placeholder="请输入价格"
              maxlength="6"
              show-word-limit
              oninput="if(isNaN(value)) { value = parseFloat(value) } if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+2)}"
            />
          </el-form-item>
          <el-form-item label="库存" style="margin-bottom: 20px" :label-width="formLabelWidth" prop="inventory">
            <el-input
              v-model="form.inventory"
              type="text"
              placeholder="请输入库存"
              maxlength="6"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input v-model="form.remark" type="textarea" autocomplete="off" />
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
            <el-input-number v-model="form.sort" :min="1" :max="10" label="描述文字" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancel1">取 消</el-button>
          <el-button type="primary" @click="promit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-table
      :data="list"
      border
      style="width: 98%"
      @selection-change="handleSelectionChange1"
    >
      <el-table-column
        align="center"
        type="selection"
        width="100"
      />
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="150"
      />
      <el-table-column
        align="center"
        prop="logo"
        label="图片"
        width="190"
      >
        <template slot-scope="scope">
          <img :src="scope.row.logo" width="40" height="40">
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="name"
        label="名称"
        width="150"
      />
      <el-table-column
        align="center"
        prop="price"
        label="价格"
        width="150"
      />
      <el-table-column
        align="center"
        prop="status"
        label="商品状态"
        width="150"
      >
        <template slot-scope="scope">
          <p
            v-if="scope.row.status==='1'"
            style="color:#189bff"
          >上架
          </p>
          <p
            v-if="scope.row.status==='-1'"
            style="color:#ff4949"
          >下架
          </p>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="classifyName"
        label="分类名称"
        width="200"
      />
      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="240"
      />
      <el-table-column
        align="center"
        prop="act"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==='-1'"
            id="sty1"
            ref="sty1"
            type="primary"
            plain
            size="mini"
            @click="putaway(scope.row.goodsId)"
          >上架
          </el-button>
          <el-button
            v-if="scope.row.status==='1'"
            id="sty2"
            type="danger"
            plain
            size="mini"
            class="mr-1"
            @click="soldout(scope.row.goodsId)"
          >下架
          </el-button>
          <el-button
            id="sty4"
            plain
            size="mini"
            class="ml-1"
            @click="changed(scope.row.goodsId)"
          ><span>修改</span></el-button>
          <el-popconfirm
            title="确定删除该商品吗？"
            @onConfirm="del(scope.row.goodsId)"
          >
            <el-button
              id="sty3"
              slot="reference"
              type="danger"
              size="mini"
              class="ml-1"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--修改商品-->
    <el-dialog top="9vh" title="修改商品" width="35%" :visible.sync="dialogFormVisible1" :before-close="handleClose">
      <el-form :model="form" :rules="rules">
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-radio v-model="radio1" label="1" @change="inpage3">无年龄限制</el-radio>
          <el-radio v-model="radio1" label="2" @change="inpage4">有年龄限制</el-radio>
        </el-form-item>
        <el-form-item v-if="show3" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-input v-model="shopdata.age" placeholder="请输入年龄" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类列表" :label-width="formLabelWidth">
          <el-select v-model="shopdata.classifyId" placeholder="请选择分类列表">
            <el-option v-for="(item,index) in classifyids" :key="index" :label="item.classifyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品图" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-upload
            ref="upload"
            class="avatar-uploader"
            action="https://app.xiangban365.com/app_mobile/upload/file"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="shopdata.logo" :src="shopdata.logo" class="avatar">
            <img v-else :src="shopdata.logo" class="avatar">
          </el-upload>
        </el-form-item>
        <el-form-item label="库存" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-input v-model="shopdata.inventory" autocomplete="off" />
        </el-form-item>
        <el-form-item label="商品名" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-input v-model="shopdata.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="价格" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-input v-model="shopdata.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="shopdata.remark" type="textarea" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="shopdata.sort" :min="1" :max="99" label="排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="ml-5" @click="handleCancel2">取 消</el-button>
        <el-button type="primary" class="ml-auto" @click="save(shopdata.goodsId)">修改</el-button>
      </div>

    </el-dialog>
    <!--分页-->
    <el-pagination
      background
      :current-page="prolist.pageNum"
      :page-sizes="[10, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="prolist.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handlePrevClick"
      @next-click="handleNextClick"
    />

  </div>
</template>
<script>
import { getGoodsList, getGoodsInfo, addOrUpdateGoods, alterGoods, getClassifyGoodsList } from '@/api/product'
export default {
  name: 'Product',
  data() {
    return {
      classify: {
        shopId: 4,
        name: '',
        classifyId: '',
        status: ''
      },
      options: [{
        age: '',
        value: '1',
        label: '上架'
      }, {
        value: '-1',
        label: '下架'
      }],
      radio1: '1',
      radio: 3,
      show4: false,
      show3: false,
      rules: {
        name: [
          { required: true, style: 'height:20px', message: '请输入商品名称', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        inventory: [
          { required: true, trigger: 'blur', message: '库存不能为空' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '添加图片', trigger: 'blur' },
          { trigger: 'blur' }
        ],
        classifyName: [
          { required: true, message: '添加分类', trigger: 'blur' },
          { trigger: 'blur' }
        ],
        sort: [
          { required: true, trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ]
      },
      delshow: false,
      show: false,
      show1: true,
      pageNum: 1,
      pageSize: 10,
      prolist: '',
      ids: '',
      showDel1: false,
      imageUrl1: '',
      classifyids: '',
      value1: '',
      value2: '',
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogTableVisible1: false,
      dialogFormVisible1: false,
      shopdata: '',
      imageUrl: '',
      input: '',
      form: {
        classifyName: '',
        id: '',
        name: '',
        input1: '',
        status: '',
        classifyId: '',
        goodsId: '',
        inventory: '',
        logo: '',
        price: '',
        remark: '',
        shopid: '',
        sort: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        updateTime: '',
        createTime: ''
      },
      formLabelWidth: '80px',
      list: '',
      multipleSelection: [],
      page: 1,
      page_count: 0,
      title: '',
      seven_days: []
    }
  },
  created() {
    this.change()
    this.getcateid()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    addad() { // 新增
      this.radio = 3
      this.dialogFormVisible = true
      this.form.input1 = ''
      this.form.id = ''
      this.form.inventory = ''
      this.form.name = ''
      this.form.price = ''
      this.form.remark = ''
      this.form.sort = ''
      this.imageUrl = ''
      this.inpage1()
    },
    inpage2() {
      console.log(this.form.input1)
      this.show4 = true
      if (this.form.input1 === 0) {
        this.form.input1 = ''
      } else {
        // this.form.input1 = this.form.input1
      }
    },
    inpage1() {
      this.show4 = false
    },
    inpage3() {
      this.show3 = false
    },
    inpage4() {
      console.log(this.shopdata.age)
      this.show3 = true
      if (this.shopdata.age !== 0) {
        // this.shopdata.age = this.shopdata.age
      } else {
        this.shopdata.age = ''
      }
    },
    handleCancel2() {
      this.dialogFormVisible1 = false
    },
    handleCancel1() {
      this.$refs.ruleForm.clearValidate()
      this.dialogFormVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.change()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.change()
      console.log(`当前页: ${val}`)
    },
    handlePrevClick() {
      this.pageNum--
      this.change()
    },
    handleNextClick() {
      this.pageNum++
      this.change()
    },
    handleSelectionChange1(val) {
      console.log(val.length)
      this.ids = []
      if (val.length > 0) {
        val.forEach(res => {
          this.ids.push(res.goodsId)
          console.log(this.ids)
        })
      }
      this.delshow = val.length > 0
    },
    // 批量删除
    delselect() {
      const data = {
        goodsIds: this.ids,
        status: '-2'
      }
      alterGoods(data).then(res => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.change()
        }
      })
    },
    visal() {
      console.log(this.radio)
      if (this.radio === 6) {
        this.$refs.inp1.style.display = 'block'
      }
    },
    // 获取分类列表
    getcateid() {
      const data = {
        'shopId': 4
      }
      getClassifyGoodsList(data).then(res => {
        console.log(res.dataMap.list)
        this.classifyids = res.dataMap.list
      })
    },
    // 上架
    putaway(goodsId) {
      const data = {
        goodsIds: [],
        status: '1'
      }
      data.goodsIds = []
      data.goodsIds.push(goodsId)
      alterGoods(data).then(res => {
        console.log(res)
        if (res.success) {
          this.change()
          this.$message({
            message: '上架成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 下架
    soldout(goodsId) {
      const data = {
        goodsIds: [],
        status: '-1'
      }
      data.goodsIds = []
      data.goodsIds.push(goodsId)
      alterGoods(data).then(res => {
        if (res.success) {
          this.change()
          this.$message({
            message: '下架成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
        console.log(res)
      })
    },
    // 修改
    changed(goodsId) {
      this.show3 = false
      this.dialogFormVisible1 = true
      this.shopdata = []
      const data = {
        goodsId: goodsId
      }
      getGoodsInfo(data).then(res => {
        if (res.success) {
          this.shopdata = res.dataMap
          this.radio1 = this.shopdata.age === 0 ? '1' : '2'
          this.show3 = this.shopdata.age !== 0
        }
      })
    },
    // 修改保存
    save(goodsId) {
      console.log(this.shopdata.age)
      if (this.show3 === false) {
        this.shopdata.age = 0
      }
      const data = {
        'age': this.shopdata.age,
        'classifyId': this.shopdata.classifyId,
        'createTime': this.shopdata.createTime,
        'goodsId': this.shopdata.goodsId,
        'inventory': this.shopdata.inventory,
        'name': this.shopdata.name,
        'price': this.shopdata.price,
        'remark': this.shopdata.remark,
        'sort': this.shopdata.sort,
        'logo': this.shopdata.logo
      }
      addOrUpdateGoods(data).then(res => {
        if (res.success) {
          console.log(res)
          this.dialogFormVisible1 = false
          this.change()
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 删除
    del(goodsId) {
      const data = {
        goodsIds: [],
        status: '-2'
      }
      data.goodsIds = []
      data.goodsIds.push(goodsId)
      alterGoods(data).then(res => {
        if (res.success) {
          this.change()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 商品列表的展示
    change() {
      getGoodsList(this.classify, this.pageNum, this.pageSize).then(res => {
        console.log(res)
        this.prolist = res.dataMap
        this.list = res.dataMap.list
      })
    },
    // 查询
    search() {
      this.pageNum = 1
      this.change()
    },
    handleAvatarSuccess1(res, file) {
      console.log(res)
      this.shopdata.logo = res.dataMap
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = res.dataMap
    },
    handleAvatarError(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension =
        testmsg === 'jpg' ||
        testmsg === 'JPG' ||
        testmsg === 'png' ||
        testmsg === 'PNG' ||
        testmsg === 'bpm' ||
        testmsg === 'BPM'
      const isLt50M = file.size / 1024 / 1024 < 10
      if (!extension) {
        this.$message({
          message: '上传图片只能是jpg / png / bpm格式!',
          type: 'error'
        })
        return false // 必须加上return false; 才能阻止
      }
      console.log(file)
      if (!isLt50M) {
        this.$message({
          message: '上传文件大小不能超过 10MB!',
          type: 'error'
        })
        return false
      }
      return extension || isLt50M
    },
    // 新增提交
    promit() {
      if (this.show4 === false) {
        this.form.input1 = 0
      }
      const data = {
        'age': this.form.input1,
        'classifyId': this.form.id,
        'inventory': this.form.inventory,
        'name': this.form.name,
        'price': this.form.price,
        'remark': this.form.remark,
        'sort': this.form.sort,
        'logo': this.imageUrl
      }
      addOrUpdateGoods(data).then(res => {
        if (res.success) {
          this.dialogFormVisible = false
          this.change()
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-radio-group {
  height: 65px;

}
#sty3{
  border:1px solid #ff7f5b;
}
.inp1 {
  display: none;
}

element.style {
  margin-top: 10vh;
}

.el-table td div {
  margin-left: 10px;
}

.el-form-item {
  margin-bottom: 12px;
}

.el-textarea {
  width: 300px;
}

form {
  /*width: 80%;*/
  margin: auto;
}

.el-dialog {
  width: 35%;
}

.el-input[data-v-da9eedb6][data-v-da9eedb6] {
  width: 300px;
}

.el-table {
  margin-top: 20px;
}

#sty4 {
  border:1px solid #ff7f5b;
  color: #ff7f5b;
}

#sty2 {
  /*display: none;*/
}

.el-input[data-v-da9eedb6] {
  width: 17%;
  float: left;
  overflow: hidden;
}

#sty1, #sty2, #sty3, #sty4 {
  margin-right: 0;
}

.el-pagination {
  float: right;
}

.text {
  float: left;
}

.el-button--text {
  float: left;
  border: 1px solid lightgray;
  color: black;
  margin-left: 10px;
}

#head {
  width: 100%;
  height: 30px;
}

.el-table .cell {
  padding-left: 42px;
}

.el-range-editor.el-input__inner {
  float: left;
  margin-left: 20px;
  margin-bottom: 30px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-input {
  width: 50%;
}

.el-dialog {
  margin-bottom: 9px;

}
</style>

